from PyQt5.QtWidgets import *
from PyQt5.QtCore import *
from PyQt5.QtGui import *
from PyQt5.uic import loadUiType
import sys
import Main
import pychangepassword,dbutil


#注册界面
rg, _ = loadUiType('register.ui')
class RegisterApp(QMainWindow,rg):
    def __init__(self):
        QWidget.__init__(self)
        self.setupUi(self)
        self.registerAction.setEnabled(False)
        self.changepasswordAction.setEnabled(True)
        self.loginAction.setEnabled(True)
        self.logoutAction.setEnabled(False)
        self.exitAction.setEnabled(True)
        self.menu.triggered[QAction].connect(self.menuTriggered)
        self.accountregisterPushButton.clicked.connect(self.add_users)

    def menuTriggered(self, q):
        if (q.text() == "修改密码"):
            self.change_password_app =pychangepassword.Change_passwordApp()
            self.close()
            self.change_password_app.show()

        if (q.text() == "登录"):
            self.login_app = Main.LoginApp()
            self.close()
            self.login_app.show()


        if (q.text() == "退出"):
            app = QApplication.instance()
            app.quit()
        return

    def add_users(self):

        id = self.accountnumberLineEdit.text()
        name = self.usernameLineEdit.text()
        password = self.setpasswordLineEdit.text()
        confirm_password = self.confirmpasswordLineEdit.text()

        if (id=='' or name=='' or password=='' or confirm_password==''):
            QMessageBox.warning(self, "警告", "不能为空！", QMessageBox.Yes)
            self.statusBar().showMessage("请重新输入！！")
            self.accountnumberLineEdit.setText('')
            self.usernameLineEdit.setText('')
            self.setpasswordLineEdit.setText('')
            self.confirmpasswordLineEdit.setText('')

        else:
            # 获取连接
            conn = dbutil.get_conn()
            # 获取cursor
            cur = conn.cursor()
            # SQL语句
            sql1 = "select * from users where id=%s"
            cur.execute(sql1, (id,))
            data=cur.fetchone()
            if data:
                QMessageBox.warning(self,"警告","该账号已存在!",QMessageBox.Yes)
                self.statusBar().showMessage("请重新输入账号！！")
                self.accountnumberLineEdit.setText('')
            else:
                # 获取连接
                conn = dbutil.get_conn()
                # 获取cursor
                cur = conn.cursor()
                # SQL语句
                sql = "insert into users(id,name,password) values(%s,%s,%s)"
                if (password == confirm_password):
                #执行语句
                    cur.execute(sql, (id, name, password))
                    # insert,update,delete必须显示提交
                    conn.commit()
                    # 关闭资源
                    dbutil.close_conn(conn, cur)
                    # 消息提示
                    self.statusBar().showMessage('注册成功！')
                    self.accountnumberLineEdit.setText('')
                    self.usernameLineEdit.setText('')
                    self.setpasswordLineEdit.setText('')
                    self.confirmpasswordLineEdit.setText('')
                elif (password != confirm_password):
                    QMessageBox.warning(self, "警告", "两次密码不一致！！", QMessageBox.Yes)
                    self.statusBar().showMessage("请重新输入密码！！")
                    self.setpasswordLineEdit.setText('')
                    self.confirmpasswordLineEdit.setText('')



def main():
    app = QApplication([])     # 创建应用程序 创建一个QApplication，也就是你要开发的软件app
    window = RegisterApp()         # 创建主窗口  创建一个MainAPP，用来装载你需要的各种组件、控件
    window.show()              # 显示窗口 执行QMainWindow的show()方法，显示这个QMainWindow
    app.exec_()                # 使用exit()或者点击关闭按钮退出QApplication

if __name__ == '__main__':
    main()
